import 'package:get/get.dart';
import 'package:meiyunapp/database/main_db.dart';
import 'package:meiyunapp/database/table/user_table.dart';

class DBService extends GetxService {
  FloorFlutterDatabase? _database;

  @override
  void onInit() {
    super.onInit();
    init();
  }

  void init() async {
    _database = await $FloorFloorFlutterDatabase
        .databaseBuilder('flutter_database.db')
        .build();
  }

  void insertUser(UserTable table) async {
    final userDao = _getDataBase().mUserDao;

    var tab = UserTable(message: "test1");
    userDao.insertUserTable(tab);
  }

  Future<List<UserTable>> queryAllUserTable() {
    return _getDataBase().mUserDao.findAllPersons();
  }

  FloorFlutterDatabase _getDataBase() {
    if (_database == null) {
      init();
    }
    return _database!;
  }

  @override
  void onClose() {
    _database = null;
    super.onClose();
  }
}
